﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCSLibrary;
using System.Data;
using Utils.DatabaseUtils;
using Utils.DC.Web.UI;
using Telerik.Web.UI;

public partial class quan_tri_thuoc_tinh : System.Web.UI.UserControl
{
    #region Cac bien dung chung
    PropertiesUI objProperties = new PropertiesUI();
    DataSet dsProperties;

    #endregion

    #region Cac Ham Su Kien

    protected void Page_Load(object sender, EventArgs e)
    {
        dsProperties = objProperties.GetAll();
        if (!IsPostBack)
        {
            BindTypeNameList();
        }
    }

    protected void cboPropertiesTypeName_SelectedIndexChanged(object sender, EventArgs e)
    {
        string typeName = cboPropertiesTypeName.SelectedValue;
        BindPropertiesList(typeName);
    }

    protected void Properties_Delete(object sender, EventArgs e)
    {
        try
        {
            LinkButton button = (LinkButton)sender;
            int result = objProperties.DeleteByID(Convert.ToInt32(button.CommandArgument));
            if (result > 0)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                lblMessage.Text = MessageBox.MESSAGE_DELETE_SUCCESSFULLY;
                BindTypeNameList();
            }
            else
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                lblMessage.Text = MessageBox.MESSAGE_DELETE_NOT_SUCCESSFULLY;
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
        }
    }

    protected void Ajax_Request(object sender, AjaxRequestEventArgs e)
    {
        if (e.Argument == "Rebind")
        {
            dsProperties = objProperties.GetAll();
            BindPropertiesList(cboPropertiesTypeName.SelectedValue.Trim());
        }
    }

    #endregion

    #region Cac Ham Tuong Tac Du Lieu

    private void BindPropertiesList(string typeName)
    {
        DataTable dt = TableLib.CreateTable(dsProperties.Tables[0], "PropertyTypeName = '" + typeName + "'");
        grdProperties.DataSource = dt;
        grdProperties.DataBind();
    }

    private void BindTypeNameList()
    {
        if (dsProperties != null && dsProperties.Tables.Count == 2)
        {
            cboPropertiesTypeName.DataSource = dsProperties.Tables[1];
            cboPropertiesTypeName.DataBind();
        }

        if (cboPropertiesTypeName.Items.Count > 0)
        {
            BindPropertiesList(cboPropertiesTypeName.Items[0].Value);
        }
    }



    #endregion

}
